/*
 * FrmComandas.java
 *
 * Created on 25/07/2011, 21:10:56
 */
package siscom.frames;

import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import net.java.balloontip.BalloonTip;
import net.java.balloontip.styles.EdgedBalloonStyle;
import net.java.balloontip.utils.TimingUtils;
import siscom.componentes.SisFrame;
import siscom.componentes.SisTextField;
import siscom.modelos.Comanda;
import siscom.modelos.DAO.ComandaDAO;
import siscom.util.Configuracoes;

/**
 *
 * @author Carlos Ribeiro
 */
public class FrmComandas extends SisFrame {

    public FrmComandas() {
        initComponents();
        listaComandas.getLista().addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() >= 2) {
                    JList lista = (JList) e.getSource();
                    if (lista.getSelectedValue() instanceof Comanda) {
                        Comanda comanda = (Comanda) lista.getSelectedValue();
                        gerenciarComanda(comanda);
//						refreshComandas();
                    }
                }
                super.mouseClicked(e);
            }
        });

        listaComandas.getLista().addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    JList lista = (JList) e.getSource();
                    if (lista.getSelectedValue() instanceof Comanda) {
                        Comanda comanda = (Comanda) lista.getSelectedValue();
                        gerenciarComanda(comanda);
//						refreshComandas();
                    }
                }
            }
        });
        //setTitulo("TITULO");
        setTitle("Comandas");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTitulo = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        lblComanda = new javax.swing.JLabel();
        txtComanda = new siscom.componentes.SisTextField();
        listaComandas = new siscom.componentes.SisListComandas(Configuracoes.getTotalComandas());

        lblTitulo.setFont(new java.awt.Font("Segoe Print", 1, 20)); // NOI18N
        lblTitulo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siscom/icones/table-64.png"))); // NOI18N
        lblTitulo.setText("Comandas");

        lblComanda.setDisplayedMnemonic('C');
        lblComanda.setLabelFor(txtComanda);
        lblComanda.setText("Comanda:");

        txtComanda.setApenasNumeros(true);
        txtComanda.setTamanhoMaximo(3);
        txtComanda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtComandaActionPerformed(evt);
            }
        });
        txtComanda.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtComandaFocusGained(evt);
            }
        });
        txtComanda.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtComandaKeyPressed(evt);
            }
        });

        listaComandas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listaComandasMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitulo)
                .addContainerGap(853, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 1036, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblComanda, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtComanda, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(876, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listaComandas, javax.swing.GroupLayout.DEFAULT_SIZE, 1012, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(lblTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblComanda)
                    .addComponent(txtComanda, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(listaComandas, javax.swing.GroupLayout.PREFERRED_SIZE, 433, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(89, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtComandaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtComandaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtComandaActionPerformed
    private void gerenciarComanda(Comanda comanda) {
        try {
            ComandaDAO dao = new ComandaDAO();
            dao.refreshComanda(comanda);
            if (!comanda.isAberta()) {
                if (JOptionPane.showConfirmDialog(null,
                        "Comanda fechada, gostaria de abrir?") == JOptionPane.YES_OPTION) {
                    comanda.setAberta(true);
                    FrmGerenciaComanda frmComanda = new FrmGerenciaComanda(comanda, true, this);
                    frmComanda.setLocationRelativeTo(null);
                    frmComanda.setVisible(true);
                }
            } else {
                FrmGerenciaComanda frmComanda = new FrmGerenciaComanda(comanda, true, this);
                frmComanda.setLocationRelativeTo(null);
                frmComanda.setVisible(true);
            }
            refreshComandas();
        } catch (SQLException ex) {
            Logger.getLogger(FrmComandas.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void refreshComandas() {
        try {
            listaComandas.limpa();
            ComandaDAO dao = new ComandaDAO();
            for (Comanda c : dao.selecionarTodos()) {
                c.setAberta(true);
                listaComandas.setComanda(c.getNumero(), c);
            }
            listaComandas.updateUI();
        } catch (SQLException ex) {
            siscom.util.Util.trataErro(ex);
        }
    }
    private void listaComandasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaComandasMouseClicked
    }//GEN-LAST:event_listaComandasMouseClicked

    private void txtComandaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtComandaFocusGained
        SisTextField componente = (SisTextField) evt.getSource();
        componente.setSelectionStart(0);
        componente.setSelectionEnd(componente.getText().length());
    }//GEN-LAST:event_txtComandaFocusGained

    private void txtComandaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtComandaKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            Comanda comanda = listaComandas.getComanda(txtComanda.getText().trim());
            if (comanda == null) {
                EdgedBalloonStyle style = new EdgedBalloonStyle(Color.WHITE, Color.BLUE);
                BalloonTip balloon = new BalloonTip(txtComanda, new JLabel("Comanda inválida"), style, BalloonTip.Orientation.LEFT_ABOVE, BalloonTip.AttachLocation.ALIGNED, 30, 10, false);
                TimingUtils.showTimedBalloon(balloon, 3000);
//						try {
//							Thread.sleep(6000);
//						} catch (InterruptedException e1) {
//							e1.printStackTrace();
//						}
//						balloon.setVisible(false);
                txtComanda.requestFocus();
                txtComanda.setSelectionStart(0);
                txtComanda.setSelectionEnd(txtComanda.getText().length());
                
                return;

            }
            listaComandas.getLista().setSelectedValue(comanda, true);
            listaComandas.getLista().requestFocus();
        }
    }//GEN-LAST:event_txtComandaKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblComanda;
    private javax.swing.JLabel lblTitulo;
    private siscom.componentes.SisListComandas listaComandas;
    private siscom.componentes.SisTextField txtComanda;
    // End of variables declaration//GEN-END:variables
}
